<?php
/**
 * 飞信机器人--功能插件--文章类
 * 功能：实现文章列表，查看指定文章，谜语模块
 * 数据表：fx_articles
 * 作者： Kelvin
 * 联系： msn:shichangguo@msn.com, qq:191848423
*/
/*
 支持参数(fx_menus表 extra_params字段)：
 action=list&classid=分类id: 查看指定文章列表
 action=view&id=文章id :查看指定文章
 action=list&classid=分类id&type=riddle :谜语
*/
/*
 数据表定义:
CREATE TABLE `fx_articles` (
  `id` int(14) NOT NULL auto_increment,
  `class_id` int(14) NOT NULL,
  `title` varchar(128) NOT NULL default '',
  `content` text NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=25 DEFAULT CHARSET=utf8;
*/

class ArticleView extends CommonPlugin
{
	var $UUID='5f771c8d-d2e4-4f0a-998f-a293798b646f';
	var $pageSize=6;
	
	public function ArticleView(&$session, $pid, $extraPara="")
	{
		parent::__construct($session, $pid, $extraPara);
	}
	
	public function init($class_id)
	{
		$this->setPage($class_id,1);
		$this->setRiddleID(0);
	}
	
	private function getPage($class_id)
	{
		$page = $this->session->getValue("ArticleView{$class_id}_page");
		if( $page == 0) $page = 1;
		return $page;
	}
	
	private function setPage($class_id, $page)
	{
		$this->session->set("ArticleView{$class_id}_page", $page);
	}

	
	private function getRiddleID($class_id)
	{
		$id = $this->session->getValue("ArticleView{$class_id}_RiddleID");
		if( $id == 0) $id = 1;
		return $id;
	}
	
	private function setRiddleID($class_id, $id)
	{
		$this->session->set("ArticleView{$class_id}_RiddleID", $id);
	}	
	
	public function getList($class_id,$count)
	{
		$msg = "<Font Face='楷体' Color='-16744320' Size='10.5'>";
		$extra = "";
		$perPage = $this->pageSize;
		$pages = 1;
		$currentPage = $this->getPage($class_id);
		if($count>$perPage)
		{
			$pages = ceil($count/$perPage);
			$extra = ",(第{$currentPage}页 共{$pages}页)p-上一页 n-下一页";
			$start = $perPage * ($currentPage-1);
			$sql = "select id,class_id,title from fx_articles where class_id=$class_id order by id desc  limit $start, $perPage";
		}else 
		{
			$sql = "select id,class_id,title from fx_articles where class_id=$class_id  order by id desc ";
		}
		$retArr = $this->session->db->fetchQueryAll($sql);

		for($i=0; $i<count($retArr); $i++ )
		{
			$msg.="[".($i+1)."] ".$retArr[$i]['title']."\n";
		}
		
		$msg.="</font>";
		$msg.= $this->getFooter($extra);
		return $msg;
	}
	
	public function getTips()
	{
		$action = $this->extraParam['action'];
		if( $action== 'list')
		{
			$class_id=$this->extraParam['classid'];
			$this->init($class_id);
			
			$sql = "select count(*) as count from fx_articles where class_id=$class_id";
			$retArr = $this->session->db->fetchQueryAll($sql);
			$count = $retArr[0]['count'];
			
			return $this->getList($class_id,$count);
		}else if( $action== 'view' )
		{
			$id = $this->extraParam['id'];
			return $this->getContent($id);
		}
		return "";		
	}
	
	public function getListContent($class_id,$count,$input)
	{
		$msg = "<Font Face='楷体' Color='-16744320' Size='10.5'>";
		$extra = "";
		$perPage = $this->pageSize;
		$pages = 1;
		$currentPage = $this->getPage($class_id);
		if($count>$perPage)
		{
			$pages = ceil($count/$perPage);
			$extra = ",(第{$currentPage}页 共{$pages}页)p-上一页 n-下一页";
			$start = $perPage * ($currentPage-1);
			$sql = "select * from fx_articles where class_id=$class_id order by id desc  limit $start, $perPage";
		}else 
		{
			$sql = "select * from fx_articles where class_id=$class_id  order by id desc ";
		}
		$retArr = $this->session->db->fetchQueryAll($sql);

		for($i=0; $i<count($retArr); $i++ )
		{
			if( intval($input) == $i+1 )
			{
				$msg.=$retArr[$i]['content'];
			}
		}
		
		$msg.="</font>";
		$msg.= $this->getFooter(",l-返回列表");
		return $msg;
	}
	
	public function getContent($id)
	{
		$sql = "select * from fx_articles where id=$id";
		$retArr = $this->session->db->fetchQueryAll($sql);
		
		$msg = "";
		$msg.=$retArr[0]['content'];
		$msg.= $this->getFooter();
		return $msg;
	}
	public function riddleEnter($class_id,$count,$input)
	{
		$msg = "<Font Face='楷体' Color='-16744320' Size='10.5'>";
		$extra = "";
		$perPage = $this->pageSize;
		$pages = 1;
		$currentPage = $this->getPage($class_id);
		
		if($count>$perPage)
		{
			$pages = ceil($count/$perPage);
			$start = $perPage * ($currentPage-1);
			$sql = "select * from fx_articles where class_id=$class_id order by id desc  limit $start, $perPage";
		}else 
		{
			$sql = "select * from fx_articles where class_id=$class_id  order by id desc ";
		}
		$retArr = $this->session->db->fetchQueryAll($sql);

		for($i=0; $i<count($retArr); $i++ )
		{
			if( intval($input) == $i+1 )
			{
				$this->setRiddleID($class_id,$retArr[$i]['id']);
				break;
			}
		}
		$msg.="您选择的问题是：{$retArr[$i]['title']}\n请输入您的答案</font>";
		$msg.= $this->getFooter(",l-返回列表,v-查看答案");
		return $msg;
	}
	public function riddleShow($class_id)
	{
		$riddleID = $this->getRiddleID($class_id);
		
		$sql = "select * from fx_articles where id=$riddleID";
		$retArr = $this->session->db->fetchQueryAll($sql);
		
		$msg = "<Font Face='楷体' Color='-16744320' Size='10.5'>答案是：";
		$msg.=$retArr[0]['content'];
		$msg.="\n您答对了没有?:)</font>";
		$msg.= $this->getFooter(',l-返回列表');
		return $msg;
	}
	public function riddleDo($class_id,$input)
	{
		$riddleID = $this->getRiddleID($class_id);
		
		$sql = "select * from fx_articles where id=$riddleID";
		$retArr = $this->session->db->fetchQueryAll($sql);
		
		$msg = "<Font Face='楷体' Color='-16744320' Size='10.5'>";
		if( $input == $retArr[0]['content'] )
		{
			$msg.="太聪明了，答对了！";
		}else if( !(strpos($input,$retArr[0]['content'])===false && strpos($retArr[0]['content'],$input)===false) )
		{
			$msg.="有点接近了，努力！";
		}else 
		{
			$msg.="完全不正确，继续猜吧！";
		}
		$msg.="</font>";
		$msg.= $this->getFooter(',v-查看答案,l-返回列表');
		return $msg;	
	}
	public function handle($input)
	{
		if( parent::handleInput($input)) return false;
		
		$action = $this->extraParam['action'];
		if( $action== 'list')
		{
			$class_id=$this->extraParam['classid'];
			$extra = "";
			$perPage = $this->pageSize;
			$pages = 1;
			$currentPage = $this->getPage($class_id);
			
			$sql = "select count(*) as count from fx_articles where class_id=$class_id";
			$retArr = $this->session->db->fetchQueryAll($sql);
			$count = $retArr[0]['count'];
			
			if($count>$perPage)
			{
				$pages = ceil($count/$perPage);
			} 
						
			if( $input == 'p' )	//上一页
			{
				if($currentPage>1)
				{
					$currentPage--;
					$this->setPage($class_id, $currentPage);
				}
				return $this->getList($class_id,$count);
			}else if( $input == 'n') //下一页
			{
				if($currentPage<$pages)
				{
					$currentPage++;
					$this->setPage($class_id, $currentPage);
				}
				return $this->getList($class_id,$count);
			}else if($input == 'l')
			{
				return $this->getList($class_id,$count);
			}else if( is_numeric($input) )
			{
				$type = $this->extraParam['type'];
				if($type=='riddle') /*谜语*/
				{
					return $this->riddleEnter($class_id,$count,$input);
				}else 
				{
					return $this->getListContent($class_id,$count,$input);
				}
			}else if( $input == 'v' )
			{
				$type = $this->extraParam['type'];
				if($type=='riddle') /*谜语*/
				{
					return $this->riddleShow($class_id);
				}
			}else
			{
				$type = $this->extraParam['type'];
				if($type=='riddle') /*谜语*/
				{
					return $this->riddleDo($class_id,$input);
				}				
			}
		}else if( $action== 'view' )
		{
			$id = $this->extraParam['id'];
			return $this->getContent($id);
		}
		$msg = "系统中未发现有关 $input 的帮助内容\n";
		$msg.= $this->getFooter();
		return $msg;
	}
}
?>